package com.yichat.micro.msghttpservice.core.rpc.motan.config;

import com.weibo.api.motan.config.springsupport.AnnotationBean;
import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
import com.weibo.api.motan.config.springsupport.ProtocolConfigBean;
import com.weibo.api.motan.config.springsupport.RegistryConfigBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * Created by chenmohan on 2019/04/11
 *
 * @description
 */
@ConditionalOnExpression("${msg-http-service.rpc.motan.enable:false}")
@PropertySource(value = "classpath:config/motan.properties")
@Configuration
@Slf4j
public class MotanServerConfig {

    static {
        log.info("【STARTING】::motan rpc is enbale ,then init");
    }

    /**
     * 声明Annotation用来指定需要解析的包名
     *
     * @return AnnotationBean
     */
    @Bean
    @ConfigurationProperties(prefix = "motan.annotation")
    public static AnnotationBean motanAnnotationBean() {
        AnnotationBean motanAnnotationBean = new AnnotationBean();
        return motanAnnotationBean;
    }

    /**
     * Motan协议配置
     *
     * @return ProtocolConfigBean
     */
    @Bean(name = "motan")
    @ConfigurationProperties(prefix = "motan.protocol")
    public ProtocolConfigBean protocolConfig() {
        ProtocolConfigBean config = new ProtocolConfigBean();
        return config;
    }

    /**
     * Motan注册中心配置
     *
     * @return RegistryConfigBean
     */
    @Bean(name = "registryConfig")
    @ConfigurationProperties(prefix = "motan.registry")
    public RegistryConfigBean registryConfig() {
        RegistryConfigBean config = new RegistryConfigBean();
        config.setRegProtocol("zookeeper");
        return config;
    }

    /**
     * Motan服务端配置
     *
     * @return BasicServiceConfigBean
     */
    @Bean
    @ConfigurationProperties(prefix = "motan.server")
    public BasicServiceConfigBean baseServiceConfig() {
        BasicServiceConfigBean config = new BasicServiceConfigBean();
        return config;
    }

}
